import React, { useState, useEffect } from 'react'; 
import { createReport,editProduct,separatedFm,addFm,editFm,separatedFix,separatedCreateReport,subjectTestResult,updateSubjectTestResult,mixedfm,mixedFix,mixedCreateReport,getRmInciResult,saveSafetyRmInciResult,batchSaveRmInciResult,updateDataResult,uploadRemark,uploadRemarkUpdate,delRemark,refreshResult} from "@/api/modules/jhreport";
import { Button, Space, Typography,Row,Col } from 'antd';
import {useTranslation} from 'react-i18next'
const { Title, Paragraph } = Typography;  
const Step6 = ({ formData,onUploadSuccess }) => {
    const { t } = useTranslation();
    const [reportData, setReportData] = useState([]);  
    const [isButtonDisabled, setIsButtonDisabled] = useState(false);  
    const [isLoading, setIsLoading] = useState(false);  
    const [isReportGenerating, setIsReportGenerating] = useState(false); // 控制报告是否正在生成的状态  
    const [timeoutOccurred, setTimeoutOccurred] = useState(false); // 控制是否发生超时的状态  

    const generateReport = () =>{
        setTimeoutOccurred(true)
        setIsReportGenerating(true);
            createReport(formData,false).then(res=>{
            if (res.data.status == 0) {
                setIsReportGenerating(false); // 无论成功还是失败，都停止生成报告状态 
                console.log("report",res.data);
                let date = new Date(); // 获取当前时间 
                const adjustedDate = new Date(date.getTime() + 8 * 60 * 60 * 1000);  
                let timeString = adjustedDate.toISOString().slice(0, 19).replace('T', ' '); 
                let report= {
                    name: 'CPSR in Word format'+timeString,
                    link: res.data.data.replace("api", "res-dir")
                    }
                setReportData(prevData => [...prevData, report]); // 使用展开运算符追加新数据 
                //loading.close()
            } else {
                setIsReportGenerating(false)
                //loading.close()
                }
            }).catch(() => {
                setIsReportGenerating(false)
            });
    }
  // 渲染下载链接  
  const renderDownloadLink = (name, link) => {  
    return (  
      <Button type='primary' style={{marginLeft:'20px'}} href={link}>  
          {t('download')} 
      </Button>  
    );  
  };  
  
  // 渲染报告项  
  const renderReportItems = () => {  
    return reportData.map((item, index) => (  
      <div key={index} style={{marginTop:'2%'}}>  
        <Space>  
          <span>{item.name}</span>  
          {renderDownloadLink(item.name, item.link)}  
        </Space>  
      </div>  
    ));  
  };  
  
  return (  
    <div>  
      <Row justify="center" align="middle">  
        <Col>  
        <Button  
            type="primary"  
            onClick={generateReport}  
            disabled={isReportGenerating}  
        >  
            {t('report.GeneratingReport')}  
        </Button>  
        </Col>  
    </Row>   

      {timeoutOccurred && (  
        <Paragraph style={{ marginTop: '16px' }}>  
            {t('report.ReportinGenerating')}  
        </Paragraph>  
      )}  
      {reportData.length > 0 && (  
        <div style={{ marginTop: '16px' }}>  
          {renderReportItems()}  
        </div>  
      )}  
    </div>  
  );  
}; 
export default Step6
